package com.emailsystem.emailsystem.dao.Impl;

import com.emailsystem.emailsystem.domain.Completion;
import com.emailsystem.emailsystem.domain.Connection;
import com.emailsystem.emailsystem.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Mapper
@Transactional

public interface UserDAO {

    //新
    @Select("select password from tb_user where account=#{account}")
    String selectPasswordByAccount(String account);

    @Select("SELECT count(*) FROM tb_user where account=#{account} limit 1")
    Integer testUserByAccount(String account);

    @Select("insert into tb_user(account,password) values(#{account},#{password})")
    void insert(String account, String password);
    @Update("update tb_user set password=#{password} where account=#{account}")
    void update(String account, String password);

    @Update("update tb_user set password=#{password} where account=#{account}")
    void updatePassword(String account, String password);

    @Select("select * from tb_user where account=#{account}")
    User selectUserByAccount(String account);


//    @Insert("insert into tb_user(userID,nickname,account,password) values(#{userID},#{nickname},#{account},#{password})")
//    boolean insertCompletion(String userID, String nickname, String account, String password);

    @Insert("insert into tb_connection(connectionID,account,IP,port,protocol) values(#{connectionID},#{account},#{IP},#{port},#{protocol})")
    boolean insertConnection(String connectionID,String account, String IP, Integer port,String protocol);

    /**
     * 靠账号和服务器的ip修改，这个IP的端口和协议
     * @param account
     * @param IP
     * @param port
     * @param protocol
     * @return
     */
    @Update("update tb_connection set port=#{port},protocol=#{protocol} where account=#{account} and IP=#{IP}")
    boolean updateConnetion(String account, String IP, Integer port,String protocol);

    /**
     * 获取用户的connection
     * @param account
     * @return
     */
    @Select("select * from tb_connection where account=#{account}")
    ArrayList<Connection> getConnectionByAccount(String account);
    @Select("select * from tb_completion where account=#{account} limit 1")
    Completion getCompletionByAccount(String account);

    @Update("update tb_completion set sent_email=#{sentEmail},receive_email=#{receiveEmail},favourite_email=#{favouriteEmail},draft_email=#{draftEmail},rubbish_email=#{rubbishEmail} where account=#{account}")
    void updateCompletion(String sentEmail,String receiveEmail,String favouriteEmail,String draftEmail,String rubbishEmail,String account);

    @Update("update tb_completion set receive_email=#{receiveEmail} where account=#{account}")
    void updateCompletionOnlyReceived(String receiveEmail,@Param("account") String account);

    @Insert("insert into tb_completion(account,receive_email) values(#{account},#{receiveEmail}) ")
    void insertCompletionInPOP3(@Param("account")String account, String receiveEmail);

    @Insert("insert into tb_completion(account,receive_email,sent_email,draft_email,favourite_email,rubbish_email) values(#{account},#{receiveEmail},#{sentEmail},#{draftEmail},#{favouriteEmail},#{rubbishEmail}) ")
    void insertCompletionInIMAP(@Param("account")String account,String receiveEmail,String sentEmail,String draftEmail,String favouriteEmail,String rubbishEmail);
}
